package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.utils.AssertUtil;
import com.vincent.common.utils.ResultMapUtil;
import com.vincent.dao.*;
import com.vincent.model.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Vincent on 2014/12/20.
 */
@Service("decoratorService")
public class DecoratorService {

    @Resource
    private SystemSettingDao systemSettingDao;
    @Resource
    private AdDao adDao;
    @Resource
    private AdPostionDao adPostionDao;
    @Resource
    private NavigationDao navigationDao;
    @Resource
    private HotSearchDao hotSearchDao;
    @Resource
    private MenuService menuService;


    /**
     * 前端 网站模板
     *
     * @return
     */
    public Map<String, Object> findShopDecorator() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取系统设置
        SystemSettingWithBLOBs systemSetting = systemSettingDao.getLatest();
        //获取显示广告
        List<Ad> adList = adDao.selectAll();
        //显示广告位置
        List<AdPosition> adPositionList = adPostionDao.selectAll();
        for (AdPosition adPosition : adPositionList) {
            List<Ad> ads = new ArrayList<Ad>();
            for (Ad ad : adList) {
                if (ad.getPostion().intValue() == adPosition.getId().intValue()) {
                    ads.add(ad);
                }
            }
            adPosition.setAdChildren(ads);
        }
        Map<String, Object> adMap = new HashMap<String, Object>();
        adMap.put("ads", adList);
        adMap.put("adposition", adPositionList);

        //导航
        List<Navigation> navigationList = navigationDao.selectAll();
        Map<String, Object> navigationMap = new HashMap<String, Object>();
        navigationMap.put("top", getNavigationListByType(0, navigationList));
        navigationMap.put("foot", getNavigationListByType(2, navigationList));
        navigationMap.put("main", getNavigationListByType(1, navigationList));

        //热门搜索
        List<String> topSearches = hotSearchDao.selectTop(5);

        //数据对象
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("systemsetting", systemSetting);
        dataMap.put("adMap", adMap);
        dataMap.put("navigation", navigationMap);
        dataMap.put("hotSearches", topSearches);
        resultMap.put(ResultMapKey.SUCCESS.getCode(), dataMap);
        return resultMap;
    }


    /**
     * 管理员登陆模板
     *
     * @return
     */
    public Map<String, Object> findAdminLoginDecorator() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取系统设置
        SystemSetting systemSetting = systemSettingDao.getLatest();
        //数据对象
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("systemsetting", systemSetting);

        resultMap.put(ResultMapKey.SUCCESS.getCode(), dataMap);
        return resultMap;
    }

    /**
     * 管理员后台模板
     *
     * @return
     */
    public Map<String, Object> findAdminLoginSuccessDecorator() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //获取系统设置
        SystemSetting systemSetting = systemSettingDao.getLatest();

        Map<String, Object> menuMap = menuService.findMenus();
        Object successObj = ResultMapUtil.getSuccessObj(menuMap);
        AssertUtil.notNull(successObj);
        //数据对象
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("systemsetting", systemSetting);
        dataMap.put("menus", ((Map<String,Object>)successObj).get("rows"));

        resultMap.put(ResultMapKey.SUCCESS.getCode(), dataMap);
        return resultMap;
    }

    /**
     * 分别获取不同部位的导航
     *
     * @param type
     * @param navigationList
     * @return
     */
    private List<Navigation> getNavigationListByType(int type, List<Navigation> navigationList) {
        List<Navigation> list = new ArrayList<Navigation>();
        for (Navigation navigation : navigationList) {
            if (navigation.getPosition().intValue() == type) {
                list.add(navigation);
            }
        }
        return list;
    }
}
